import java.lang.reflect.Array;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

/*
    list集合在java7默认的大小为10 java8默认为空引用，在第一次add的元素的时候才会去创建一个真正集合大小为10
    map集合的默认大小为16，每次扩容大小增加1倍
    集合类在多线程下的不安全问题:
        出现java.util.ConcurrentModificationException异常
 */
public class NotSafeDemo {
    public static void main(String[] args) {
        Map<Integer,String> map=new ConcurrentHashMap<>();
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            new Thread(()->{
                map.put(finalI +1,UUID.randomUUID().toString().substring(0,8));
                System.out.println(map);
            }).start();
        }
    }

    public static void notSafeList(){
        //List<String> list=new ArrayList<>();
//        List<String> list=new Vector<>();
//        List<String> list= Collections.synchronizedList(new ArrayList<>());
        List<String> list= new CopyOnWriteArrayList<>();
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                list.add(UUID.randomUUID().toString().substring(0,8));
                System.out.println(list);
            }).start();
        }
    }

    public static void notSafeSet(){
        Set<String> set=new CopyOnWriteArraySet<>();
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                set.add(UUID.randomUUID().toString().substring(0,8));
                System.out.println(set);
            }).start();
        }
    }
}
